//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package sto.android.app;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.os.Build.VERSION;
import android.util.Log;
import com.zebra.adc.decoder.BarCodeReader;
import com.zebra.adc.decoder.BarCodeReader.DecodeCallback;
import sto.android.app.StoJNI.ScanCallBack;

public class KaiScanner2D implements DecodeCallback {
    private BarCodeReader bcr = null;
    private int trigMode = 0;
    static final int STATE_IDLE = 0;
    static final int STATE_DECODE = 1;
    static final int STATE_HANDSFREE = 2;
    private int state = 0;
    private int mTimeOut = 10;
    private Handler mHander = new Handler() {
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(KaiScanner2D.this.state != 0) {
                KaiScanner2D.this.doStop();
            }

        }
    };
    ScanCallBack mScan;

    static {
        System.loadLibrary("IAL");
        System.loadLibrary("SDL");
        if(VERSION.SDK_INT >= 19) {
            System.loadLibrary("barcodereader44");
        } else if(VERSION.SDK_INT >= 18) {
            System.loadLibrary("barcodereader43");
        } else {
            System.loadLibrary("barcodereader");
        }

    }

    public void onDecodeComplete(int symbology, int length, byte[] data, BarCodeReader reader) {
        if(length > 0) {
            if(!this.isHandsFree() && !this.isAutoAim()) {
                this.bcr.stopDecode();
            }

            if(symbology == 153) {
                symbology = data[0];
                byte str = data[1];
                int s = 2;
                int d = 0;
                boolean len = false;
                byte[] d99 = new byte[data.length];

                for(int i = 0; i < str; ++i) {
                    s += 2;
                    byte var12 = data[s++];
                    System.arraycopy(data, s, d99, d, var12);
                    s += var12;
                    d += var12;
                }

                d99[d] = 0;
                data = d99;
            }

            if(this.mScan != null) {
                String var11 = new String(data);
                var11 = var11.substring(0, length);
                this.mScan.onScanResults(var11, symbology);
                this.mScan.onScanResults(var11);
            }
        } else {
            switch(length) {
            case -2:
            default:
                System.out.println("decode failed");
                break;
            case -1:
                System.out.println("decode cancelled");
                break;
            case 0:
                System.out.println("decode timed out");
                if(this.mScan != null) {
                    this.mScan.onScanResults((String)null);
                }
            }
        }

        if(this.state == 1) {
            this.state = 0;
        }

        this.mHander.removeMessages(0);
    }

    public void onEvent(int event, int info, byte[] data, BarCodeReader reader) {
    }

    private boolean isHandsFree() {
        return this.trigMode == 7;
    }

    private boolean isAutoAim() {
        return this.trigMode == 9;
    }

    private int doSetParam(int num, int val) {
        String s = "";
        int ret = this.bcr.setParameter(num, val);
        if(ret != -1) {
            if(num == 138) {
                this.trigMode = val;
                if(val == 7) {
                    s = "HandsFree";
                } else if(val == 9) {
                    s = "AutoAim";
                    ret = this.bcr.startHandsFreeDecode(9);
                    if(ret != 0) {
                        System.out.println("AUtoAIm start FAILED");
                    }
                } else if(val == 0) {
                    s = "Level";
                }
            }
        } else {
            s = " FAILED (" + ret + ")";
        }

        System.out.println("Set #" + num + " to " + val + " " + s);
        return ret;
    }

    private int setIdle() {
        int prevState = this.state;
        int ret = prevState;
        this.state = 0;
        switch(prevState) {
        case 1:
            break;
        case 2:
            this.resetTrigger();
            break;
        default:
            ret = 0;
        }

        return ret;
    }

    public void resetTrigger() {
        this.doStart();
    }

    public byte[] getDecImage() {
        return this.bcr.getLastDecImage();
    }

    public byte[] getDecodeImage() {
        byte[] data = this.bcr.getLastDecImage();
        return data == null?null:this.bcr.getLastDecImage();
    }

    public void doStart() {
        Log.d("HUAWEI", "KaiScanner2D *** doStart()");
        if(this.bcr != null && this.state == 0) {
            this.bcr.startDecode();
            this.state = 1;
        }

    }

    public void doPreview() {
        this.bcr.startPreview();
    }

    public void doStopPreview() {
        this.bcr.stopPreview();
    }

    public void doStop() {
        if(this.bcr != null) {
            System.out.println("slichee doStop 112233");
            this.bcr.stopDecode();
            this.state = 0;
        }

    }

    public void doSetValue(int param, int value) {
        if(this.bcr != null) {
            this.doSetParam(param, value);
        }

    }

    public void doOpen(Context mContext) {
        try {
            this.bcr = BarCodeReader.open(1, mContext.getApplicationContext());
            if(this.bcr == null) {
                Log.d("HUAWEI", "KaiScanner2D *** doOpen() *** bcr == null");
                System.out.println("open failed");
                return;
            }

            this.bcr.setDecodeCallback(this);
            this.doSetParam(765, 0);
            this.doSetParam(905, 1);
        } catch (Exception var3) {
            Log.v("sjb", "open excp:" + var3);
        }

    }

    public void doClose() {
        if(this.bcr != null) {
            this.setIdle();
            this.bcr.release();
            this.bcr = null;
        }

    }

    public void setParameter(int par) {
        this.doSetParam(par >> 16 & '\uffff', par & '\uffff');
    }

    public boolean isScanning() {
        return this.state == 1 || this.state == 2;
    }

    public void setTimeout(int sec) {
        this.mTimeOut = sec;
    }

    public KaiScanner2D() {
    }

    public void setCallback(ScanCallBack scan) {
        this.mScan = scan;
    }

    public KaiScanner2D(ScanCallBack scan) {
        this.mScan = scan;
    }
}
